<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 社区详情
 *
 * @author      Eden
 * @datetime    2016/12/10 22:46
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Detail extends ADM_Controller
{
    const PAGE_SIZE = 20;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主页面
     */
    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/bbs/detail.html');
    }

    public function content()
    {
        $send['state'] = false;
        do {
            $bbsId = $this->input->get('bbsId');
            if (empty($bbsId)) {
                $send['error'] = '详情获取失败';
                break;
            }

            $this->load->model('admin/bbs/Index_Model', 'bbs');
            $detail = $this->bbs->getBbsDetail($bbsId);
            if (empty($detail)) {
                $send['error'] = '详情获取失败';
                break;
            }

            $send['state'] = true;
            $send['data'] = $detail;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 评论列表
     */
    public function getComments()
    {
        $send['state'] = false;
        do {

            $bbsId = $this->input->get('bbsId');
            if (empty($bbsId)) {
                $send['error'] = '参数错误';
                break;
            }

            $page = $this->input->get('page');
            $page = $page > 0 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $return = array(
                'total' => 0,
                'data' => [],
                'nextPage' => false,
            );
            $this->load->model('admin/bbs/Index_Model', 'bbs');
            $lists = $this->bbs->getCommentsLists($bbsId, $offset, self::PAGE_SIZE);
            if ($lists) {
                $total = intval($this->bbs->getTotal());
                $return['total'] = $total;
                $return['data'] = $lists;
                $return['nextPage'] = ($offset + self::PAGE_SIZE) < $total;
            }

            $send['state'] = true;
            $send['data'] = $return;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 删除评论
     */
    public function deleComment()
    {
        $send['state'] =false;
        do {

            $commentId = $this->input->get('commentId');
            if (empty($commentId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Index_Model', 'bbs');
            $res = $this->bbs->deleteComment($commentId);
            if (!$res) {
                $error = $this->bbs->getError();
                $send['error'] = empty($error) ? '删除评论失败' : $error;
                break;
            }

            $send['state'] = true;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 删除回复
     */
    public function deleteReply()
    {
        $send['state'] =false;
        do {

            $replyId = $this->input->get('replyId');
            if (empty($replyId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Index_Model', 'bbs');
            $res = $this->bbs->deleteReply($replyId);
            if (!$res) {
                $error = $this->bbs->getError();
                $send['error'] = empty($error) ? '删除回复失败' : $error;
                break;
            }

            $send['state'] = true;
        } while(false);
        $this->output->send($send);
    }


}
